#if Unity_XR
using System.Collections;
using System.Collections.Generic;
using HerenityCode.Reference.SingletonPattern;
using UnityEngine;
using System;
using HerenityCode.Tool.PicoInputManager;
using UnityEngine.XR;
using UnityEngine.XR.Interaction.Toolkit;

namespace HerenityCode.Tool
{
    namespace PicoInputGet
    {
        /// <summary>
        /// Description:Pico输入获取
        /// Author:哒先生
        /// Date:2024年10月9日17:32:39
        /// </summary>
        public class InputGet : SingletonPatternMonoAutoBase<InputGet>
        {
            static bool leftTriggerDown;
            static bool leftTriggerHold;
            static bool leftTriggerUp;

            static bool rightTriggerDown;
            static bool rightTriggerHold;
            static bool rightTriggerUp;

            static bool leftGripDown;
            static bool leftGripHold;
            static bool leftGripUp;

            static bool rightGripDown;
            static bool rightGripHold;
            static bool rightGripUp;

            static bool leftAppButtonDown;
            static bool leftAppButtonHold;
            static bool leftAppButtonUp;

            static bool rightAppButtonDown;
            static bool rightAppButtonHold;
            static bool rightAppButtonUp;

            static bool leftJoyStickDown;
            static bool leftJoyStickHold;
            static bool leftJoyStickUp;

            static bool rightJoyStickDown;
            static bool rightJoyStickHold;
            static bool rightJoyStickUp;

            static bool leftAXButtonDown;
            static bool leftAXButtonHold;
            static bool leftAXButtonUp;

            static bool leftBYButtonDown;
            static bool leftBYButtonHold;
            static bool leftBYButtonUp;

            static bool rightAXButtonDown;
            static bool rightAXButtonHold;
            static bool rightAXButtonUp;

            static bool rightBYButtonDown;
            static bool rightBYButtonHold;
            static bool rightBYButtonUp;

            static bool leftJoyStickDelta;
            static bool rightJoyStickDelta;
            static Vector2 leftJoyStickMove;
            static Vector2 rightJoyStickMove;

            static GameObject leftHand;
            static GameObject rightHand;

            private void Awake()
            {
                leftHand = GameObject.Find("LeftHand Controller");
                rightHand = GameObject.Find("RightHand Controller");

                InputManager.Instance.onLeftAppButtonHold += () => { leftAppButtonHold = true; };
                InputManager.Instance.onLeftAppButtonDown += () => { leftAppButtonDown = true; };
                InputManager.Instance.onLeftAppButtonUp += () => { leftAppButtonUp = true; };
                InputManager.Instance.onLeftAXButtonHold += () => { leftAXButtonHold = true; };
                InputManager.Instance.onLeftAXButtonDown += () => { leftAXButtonDown = true; };
                InputManager.Instance.onLeftAXButtonUp += () => { leftAXButtonUp = true; };
                InputManager.Instance.onLeftBYButtonHold += () => { leftBYButtonHold = true; };
                InputManager.Instance.onLeftBYButtonDown += () => { leftBYButtonDown = true; };
                InputManager.Instance.onLeftBYButtonUp += () => { leftBYButtonUp = true; };
                InputManager.Instance.onLeftGripHold += () => { leftGripHold = true; };
                InputManager.Instance.onLeftGripDown += () => { leftGripDown = true; };
                InputManager.Instance.onLeftGripUp += () => { leftGripUp = true; };
                InputManager.Instance.onLeftJoyStickHold += () => { leftJoyStickHold = true; };
                InputManager.Instance.onLeftJoyStickDown += () => { leftJoyStickDown = true; };
                InputManager.Instance.onLeftJoyStickUp += () => { leftJoyStickUp = true; };
                InputManager.Instance.onLeftTriggerHold += () => { leftTriggerHold = true; };
                InputManager.Instance.onLeftTriggerDown += () => { leftTriggerDown = true; };
                InputManager.Instance.onLeftTriggerUp += () => { leftTriggerUp = true; };
                InputManager.Instance.onRightAppButtonHold += () => { rightAppButtonHold = true; };
                InputManager.Instance.onRightAppButtonDown += () => { rightAppButtonDown = true; };
                InputManager.Instance.onRightAppButtonUp += () => { rightAppButtonUp = true; };
                InputManager.Instance.onRightAXButtonHold += () => { rightAXButtonHold = true; };
                InputManager.Instance.onRightAXButtonDown += () => { rightAXButtonDown = true; };
                InputManager.Instance.onRightAXButtonUp += () => { rightAXButtonUp = true; };
                InputManager.Instance.onRightBYButtonHold += () => { rightBYButtonHold = true; };
                InputManager.Instance.onRightBYButtonDown += () => { rightBYButtonDown = true; };
                InputManager.Instance.onRightBYButtonUp += () => { rightBYButtonUp = true; };
                InputManager.Instance.onRightGripHold += () => { rightGripHold = true; };
                InputManager.Instance.onRightGripDown += () => { rightGripDown = true; };
                InputManager.Instance.onRightGripUp += () => { rightGripUp = true; };
                InputManager.Instance.onRightJoyStickHold += () => { rightJoyStickHold = true; };
                InputManager.Instance.onRightJoyStickDown += () => { rightJoyStickDown = true; };
                InputManager.Instance.onRightJoyStickUp += () => { rightJoyStickUp = true; };
                InputManager.Instance.onRightTriggerHold += () => { rightTriggerHold = true; };
                InputManager.Instance.onRightTriggerDown += () => { rightTriggerDown = true; };
                InputManager.Instance.onRightTriggerUp += () => { rightTriggerUp = true; };
                InputManager.Instance.onLeftJoyStickMove += (Vector2 vector) =>
                {
                    leftJoyStickMove = vector;
                    leftJoyStickDelta = vector != Vector2.zero;
                };
                InputManager.Instance.onRightJoyStickMove += (Vector2 vector) =>
                {
                    rightJoyStickMove = vector;
                    rightJoyStickDelta = vector != Vector2.zero;
                };
            }

            private void LateUpdate()
            {
                SetFalse();
            }

            private void SetFalse()
            {
                leftTriggerDown = false;
                leftTriggerHold = false;
                leftTriggerUp = false;

                rightTriggerDown = false;
                rightTriggerHold = false;
                rightTriggerUp = false;

                leftGripDown = false;
                leftGripHold = false;
                leftGripUp = false;

                rightGripDown = false;
                rightGripHold = false;
                rightGripUp = false;

                leftAppButtonDown = false;
                leftAppButtonHold = false;
                leftAppButtonUp = false;

                rightAppButtonDown = false;
                rightAppButtonHold = false;
                rightAppButtonUp = false;

                leftJoyStickDown = false;
                leftJoyStickHold = false;
                leftJoyStickUp = false;

                rightJoyStickDown = false;
                rightJoyStickHold = false;
                rightJoyStickUp = false;

                leftAXButtonDown = false;
                leftAXButtonHold = false;
                leftAXButtonUp = false;

                leftBYButtonDown = false;
                leftBYButtonHold = false;
                leftBYButtonUp = false;

                rightAXButtonDown = false;
                rightAXButtonHold = false;
                rightAXButtonUp = false;

                rightBYButtonDown = false;
                rightBYButtonHold = false;
                rightBYButtonUp = false;

                leftJoyStickDelta = false;
                rightJoyStickDelta = false;

                leftJoyStickMove = Vector2.zero;
                rightJoyStickMove = Vector2.zero;
            }

            public static bool GetKey(PicoKeyType type)
            {
                switch (type)
                {
                    case PicoKeyType.左扳机:
                        return leftTriggerHold;
                    case PicoKeyType.右扳机:
                        return rightTriggerHold;
                    case PicoKeyType.左抓握:
                        return leftGripHold;
                    case PicoKeyType.右抓握:
                        return rightGripHold;
                    case PicoKeyType.左菜单:
                        return leftAppButtonHold;
                    case PicoKeyType.右菜单:
                        return rightAppButtonHold;
                    case PicoKeyType.左AX:
                        return leftAXButtonHold;
                    case PicoKeyType.右AX:
                        return rightAXButtonHold;
                    case PicoKeyType.左BY:
                        return leftBYButtonHold;
                    case PicoKeyType.右BY:
                        return rightBYButtonHold;
                    case PicoKeyType.左Joy:
                        return leftJoyStickHold;
                    case PicoKeyType.右Joy:
                        return rightJoyStickHold;
                    case PicoKeyType.左摇杆:
                        return leftJoyStickDelta;
                    case PicoKeyType.右摇杆:
                        return rightJoyStickDelta;
                    default:
                        return false;
                }
            }

            public static bool GetKeyUp(PicoKeyType type)
            {
                switch (type)
                {
                    case PicoKeyType.左扳机:
                        return leftTriggerUp;
                    case PicoKeyType.右扳机:
                        return rightTriggerUp;
                    case PicoKeyType.左抓握:
                        return leftGripUp;
                    case PicoKeyType.右抓握:
                        return rightGripUp;
                    case PicoKeyType.左菜单:
                        return leftAppButtonUp;
                    case PicoKeyType.右菜单:
                        return rightAppButtonUp;
                    case PicoKeyType.左AX:
                        return leftAXButtonUp;
                    case PicoKeyType.右AX:
                        return rightAXButtonUp;
                    case PicoKeyType.左BY:
                        return leftBYButtonUp;
                    case PicoKeyType.右BY:
                        return rightBYButtonUp;
                    case PicoKeyType.左Joy:
                        return leftJoyStickUp;
                    case PicoKeyType.右Joy:
                        return rightJoyStickUp;
                    case PicoKeyType.左摇杆:
                        return !leftJoyStickDelta;
                    case PicoKeyType.右摇杆:
                        return !rightJoyStickDelta;
                    default:
                        return false;
                }
            }

            public static bool GetKeyDown(PicoKeyType type)
            {
                switch (type)
                {
                    case PicoKeyType.左扳机:
                        return leftTriggerDown;
                    case PicoKeyType.右扳机:
                        return rightTriggerDown;
                    case PicoKeyType.左抓握:
                        return leftGripDown;
                    case PicoKeyType.右抓握:
                        return rightGripDown;
                    case PicoKeyType.左菜单:
                        return leftAppButtonDown;
                    case PicoKeyType.右菜单:
                        return rightAppButtonDown;
                    case PicoKeyType.左AX:
                        return leftAXButtonDown;
                    case PicoKeyType.右AX:
                        return rightAXButtonDown;
                    case PicoKeyType.左BY:
                        return leftBYButtonDown;
                    case PicoKeyType.右BY:
                        return rightBYButtonDown;
                    case PicoKeyType.左Joy:
                        return leftJoyStickDown;
                    case PicoKeyType.右Joy:
                        return rightJoyStickDown;
                    case PicoKeyType.左摇杆:
                        return leftJoyStickDelta;
                    case PicoKeyType.右摇杆:
                        return rightJoyStickDelta;
                    default:
                        return false;
                }
            }

            /// <summary>
            /// 拉杆
            /// </summary>
            /// <returns></returns>
            public static Vector2 GetLeftMove()
            {
                return leftJoyStickMove;
            }

            /// <summary>
            /// 拉杆
            /// </summary>
            /// <returns></returns>
            public static Vector2 GetRightMove()
            {
                return rightJoyStickMove;
            }

            public static Ray GetLeftRay()
            {
                if (GetLeftRayActive() == false)
                {
                    return new Ray();
                }

                return new Ray(leftHand.transform.position, leftHand.transform.forward);
            }

            public static Ray GetRightRay()
            {
                if (GetRightRayActive() == false)
                {
                    return new Ray();
                }

                return new Ray(rightHand.transform.position, rightHand.transform.forward);
            }

            public static bool TryGetLeftHit(out RaycastHit hit)
            {
                if (GetLeftRayActive() == false)
                {
                    hit = new RaycastHit();
                    return false;
                }

                return leftHand.GetComponent<XRRayInteractor>().TryGetCurrent3DRaycastHit(out hit);
            }

            public static bool TryGetRightHit(out RaycastHit hit)
            {
                if (GetRightRayActive() == false)
                {
                    hit = new RaycastHit();
                    return false;
                }

                return rightHand.GetComponent<XRRayInteractor>().TryGetCurrent3DRaycastHit(out hit);
            }

            public static bool TryGetLeftPoint(out Vector3 point)
            {
                if (TryGetLeftHit(out RaycastHit hit))
                {
                    point = hit.point;
                    return true;
                }
                else
                {
                    point = Vector3.zero;
                    return false;
                }
            }

            public static bool TryGetRightPoint(out Vector3 point)
            {
                if (TryGetRightHit(out RaycastHit hit))
                {
                    point = hit.point;
                    return true;
                }
                else
                {
                    point = Vector3.zero;
                    return false;
                }
            }

            public static Transform GetLeftHand()
            {
                return leftHand.transform;
            }

            public static Transform GetRightHand()
            {
                return rightHand.transform;
            }

            public static void SetLeftRayActive(bool b)
            {
                leftHand.GetComponent<XRRayInteractor>().enabled = b;
            }

            public static void SetRightRayActive(bool b)
            {
                rightHand.GetComponent<XRRayInteractor>().enabled = b;
            }

            public static bool GetLeftRayActive()
            {
                return leftHand.GetComponent<XRRayInteractor>().enabled;
            }

            public static bool GetRightRayActive()
            {
                return rightHand.GetComponent<XRRayInteractor>().enabled;
            }
        }

        public enum PicoKeyType
        {
            左扳机,
            右扳机,
            左抓握,
            右抓握,
            左菜单,
            右菜单,
            左AX,
            右AX,
            左BY,
            右BY,
            左Joy,
            右Joy,
            左摇杆,
            右摇杆
        }
    }
}
#endif